/*
使用，在main.js注入全局，或者在指定页面使用import引入调用
使用方式：dateutil.方法名
*/

/*
参数及返回值
date: new date()
return: 2000/01/01 00:00:00
*/
function formatTime(date) {
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()

	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
/*
参数及返回值
date: new date()
return: 2000-01-01 00:00:00
*/
function formatdateTime(date) {
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()
	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()
	return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
/*
参数及返回值
date: new date()
return: 00:00
*/
function formatminTime(date) {
	var hour = date.getHours()
	var minute = date.getMinutes()

	return [hour, minute].map(formatNumber).join(':')
}
/*
参数及返回值
date: 2000-01-01T00:00:00
return: 2000/01/01
*/
function formatdatexie(date) {
	var time = new Date(Date.parse(date.replace(/-/g, "/").replace(/T/g, " ")))
	var year = time.getFullYear()
	var month = time.getMonth() + 1
	var day = time.getDate()
	return [year, month, day].map(formatNumber).join('/')
}
/*
参数及返回值
date: new date()
return: 2000年01月01日有效
*/
function formatdatetext(date) {
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()
	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()
	return year + "年" + month + "月" + day + "日有效"
}
/*
参数及返回值
date: new date()
return: 2000-01-01
*/
function formatdate(date) {
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()
	return [year, month, day].map(formatNumber).join('-')
}
/*
获取上一天
参数及返回值
date: new date()
return: 2000-01-01
*/
function formatlastdate(date) {
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate() - 1
	// var hour = date.getHours()
	// var minute = date.getMinutes()
	// var second = date.getSeconds()
	return [year, month, day].map(formatNumber).join('-')
}

function getDates(days, todate) {
	var dateArry = [];
	for (var i = 0; i < days; i++) {
		var dateObj = dateLater(todate, i);
		dateArry.push(dateObj)
	}
	return dateArry;
}
function dateLater(dates, later) {
	let dateObj = {};
	let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
	let date = new Date(dates);
	date.setDate(date.getDate() + later);
	let day = date.getDay();
	let yearDate = date.getFullYear();
	let month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
	let dayFormate = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
	dateObj.time = yearDate + '-' + month + '-' + dayFormate;
	dateObj.week = show_day[day];
	return dateObj;
}
/**
 * 判断两个时间大小，和相隔几天
 * beforeTime 2020-01-01  00:00:00
 * afterTime：2020-01-01  00:00:00
 */
function beCheckTime(beforeTime,afterTime){
	var startTime = new Date(Date.parse(beforeTime.replace(/-/g, "/").replace(/T/g, " ")));
	var endTime = new Date(Date.parse(afterTime.replace(/-/g, "/").replace(/T/g, " ")));
	if(startTime.getTime()>=endTime.getTime()){
		var obj = {
			state:false,
			day:0
		}
		return obj;
	}else{
		var obj = {
			state:true,
			day:(endTime.getTime() - startTime.getTime()) / (1000 * 60 * 60 * 24)
		}
		return obj;
	}
}
/**
 * 判断当前距离目标时间还有几天
 */
function vipCheckTime(beforeTime){
	var startTime = new Date(Date.parse(beforeTime.replace(/-/g, "/").replace(/T/g, " ")));
	var endTime = new Date();
	if(endTime.getTime()>=startTime.getTime()){
		var obj = {
			state:false,
			day:0
		}
		return obj;
	}else{
		var obj = {
			state:true,
			day:parseInt((startTime.getTime()-endTime.getTime()) / (1000 * 60 * 60 * 24))
		}
		return obj;
	}
}
/**
 * 判断时间是不是中午前
 */
function isMidTime(datetime){
	var startTime = new Date(Date.parse("2020/01/01 "+datetime+":00"));
	var hour = startTime.getHours()
	if(hour<12){
		return true;
	}else{
		return false;
	}
}

function formatNumber(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}

/**
 * 获取随机数
 */
function getrandom(min,length){
	var index = 0
	if(min){
		index = Math.floor(Math.random() * Number(length)) + Number(min);
	}else{
		index = Math.floor(Math.random() * Number(length));
	}
	return index;
}

/**
 * 将后台带T的时间变成时间戳
 *
 * @date 格式为createDate: "2020-09-25T14:31:58"
 */
function getDateTime(date) {
  var time = new Date(Date.parse(date.replace(/-/g, "/").replace(/T/g, " ")))
  return time
}

/**
 * 将后台带T的时间变成年月日
 *
 * @date 格式为createDate: "2020-09-25T14:31:58"
 */
function getprizeTime(date) {
  var time = new Date(Date.parse(date.replace(/-/g, "/").replace(/T/g, " ")))
  var year = time.getFullYear()
  var month = time.getMonth() + 1
  var day = time.getDate()
  
  var hour = time.getHours()
  var minute = time.getMinutes()
  var second = time.getSeconds()
  
  
  return formatNumber(month)+"月" + formatNumber(day)+"日"+ ' ' + [hour, minute].map(formatNumber).join(':')
}

/**
 * 计算两点距离
 */
//进行经纬度转换为距离的计算
function Rad(d) {
	return Number(d) * Math.PI / 180.0; //经纬度转换成三角函数中度分表形式。
}
//输出为公里 计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
function GetDistance(lat1, lng1, lat2, lng2) {
	var radLat1 = Rad(lat1);
	var radLat2 = Rad(lat2);
	var a = radLat1 - radLat2;
	var b = Rad(lng1) - Rad(lng2);
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
		Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * 6378.137; // EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000;
	s = s.toFixed(2);
	return s;
}

//处理距离
function dealdistance(num){
  if(Number(num)>=1000){
    return (Number(num) / 1000).toFixed(1)+"km"
  }else{
      return num.toFixed(1)+"m"
  }
}

/**
 * 将后台带T的时间替换掉
 *
 * @date 格式为createDate: "2020-09-25T14:31:58"
 */
function replaceT(date) {
  var time = date.replace(/T/g, " ")
  return time
}

module.exports = {
	formatTime: formatTime,
	formatminTime:formatminTime,
	formatdateTime: formatdateTime,
	formatdate: formatdate,
	formatdatetext: formatdatetext,
	formatlastdate: formatlastdate,
	GetDistance:GetDistance,
	beCheckTime:beCheckTime,
	vipCheckTime:vipCheckTime,
	isMidTime:isMidTime,
	replaceT:replaceT,
	getDateTime:getDateTime,
	getrandom:getrandom,
	getprizeTime:getprizeTime,
	dealdistance:dealdistance,
	formatdatexie:formatdatexie,
}
